package ac.github.os.internal.core.condition.impl.entity.damage

import ac.github.os.internal.core.condition.Condition
import ac.github.os.internal.core.condition.call
import ac.github.os.internal.core.config.StringConfig
import ac.github.os.internal.core.config.get
import ac.github.os.internal.core.holder.damage.DamageEntityHolder
import ac.github.os.internal.core.trigger.PlayerTrigger
import ac.github.os.internal.session.Session
import taboolib.common.platform.Awake

abstract class DamageEntityCondition(val name: String) : Condition {
    override fun doScreen(session: Session, config: StringConfig): Boolean {
        return if (session.holder is DamageEntityHolder) {
            run(session, config, session.holder)
        } else false
    }

    abstract fun run(session: Session, config: StringConfig, holder: DamageEntityHolder): Boolean

    override val namespace: String
        get() = "damage:$name"

    fun StringConfig.value(def: String? = null): String? = get("val", def, single = true)

}

@Awake
class DamageEntityCause : DamageEntityCondition("cause") {
    override fun run(session: Session, config: StringConfig, holder: DamageEntityHolder): Boolean {
        return config.value() == holder.event.cause.name
    }
}

@Awake
class DamageEntityDefenderType : DamageEntityCondition("defender:type") {
    override fun run(session: Session, config: StringConfig, holder: DamageEntityHolder): Boolean {
        return config.value() == holder.event.damager.type.name
    }
}

@Awake
class DamageEntityDefenderName : DamageEntityCondition("defender:name") {
    override fun run(session: Session, config: StringConfig, holder: DamageEntityHolder): Boolean {
        return config.value() == holder.event.damager.name
    }
}

@Awake
class DamageEntityAttacker : DamageEntityCondition("attacker") {
    override fun run(session: Session, config: StringConfig, holder: DamageEntityHolder): Boolean {
        return session.call<PlayerTrigger> {
            it.player == holder.damager
        }
    }
}

@Awake
class DamageEntityisProjectile : DamageEntityCondition("projectile") {
    override fun run(session: Session, config: StringConfig, holder: DamageEntityHolder): Boolean {
        return holder.isProjectile
    }
}

@Awake
class DamageEntityDefender : DamageEntityCondition("defender") {
    override fun run(session: Session, config: StringConfig, holder: DamageEntityHolder): Boolean {
        return session.call<PlayerTrigger> {
            it.player == holder.entity
        }
    }
}